package com.credit.jumu.playcard.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.credit.jumu.playcard.BuildConfig;
import com.credit.jumu.playcard.R;
import com.credit.jumu.playcard.base.BaseActivity;
import com.credit.jumu.playcard.cardapp.CardApp;
import com.credit.jumu.playcard.utils.DataSharedPreference;
import com.credit.jumu.playcard.utils.JsonUtils;
import com.credit.jumu.playcard.utils.LogUtils;
import com.credit.jumu.playcard.utils.MD5Encoder;
import com.credit.jumu.playcard.utils.SignUtil;
import com.credit.jumu.playcard.utils.SystemUtil;
import com.credit.jumu.playcard.utils.ToastUtil;
import com.credit.jumu.playcard.utils.TypeUtils;
import com.credit.jumu.playcard.utils.UrlsUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import net.sf.json.JSONObject;

import java.util.LinkedHashMap;

/**
 * Created by Administrator on 2017/10/21/021.
 */

public class SignActivity extends BaseActivity {
	TextView tofogetpass, toregister, tosign;
	private DataSharedPreference ds;
	EditText signnum;
	EditText signpass;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign);
		tofogetpass = (TextView) findViewById(R.id.tofogetpass);
		toregister = (TextView) findViewById(R.id.toregister);
		signnum = (EditText) findViewById(R.id.signnum);
		signpass = (EditText) findViewById(R.id.signpass);
		tosign = (TextView) findViewById(R.id.tosign);
		ds = new DataSharedPreference(this);
		tosign.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (TextUtils.isEmpty(signnum.getText().toString())) {
					ToastUtil.showShortToast("请先输入手机号码");
				} else if (TextUtils.isEmpty(signpass.getText().toString())) {
					ToastUtil.showShortToast("请先输入密码");
				} else {
					goSign(signnum.getText().toString(), signpass.getText().toString());
				}
			}
		});
		toregister.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SignActivity.this, RegisterActivity.class);
				startActivity(intent);
			}
		});
		tofogetpass.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SignActivity.this, LoginPassSetActivity.class);
				startActivity(intent);
			}
		});
	}

	public void goSign(String num, String pass) {
		LinkedHashMap<String, String> mapSign = new LinkedHashMap<>();
		mapSign.put("deviceCode", ds.getUUID());
		mapSign.put("intefaceType", CardApp.intefaceType);
		mapSign.put("mobile", num);
		mapSign.put("passWord", MD5Encoder.encodeBase64(pass));
		mapSign.put("source", CardApp.source);
		mapSign.put("terminalId", "A");
		mapSign.put("timeStamp", SystemUtil.getSystemTime());
		mapSign.put("version", BuildConfig.VERSION_NAME);
		mapSign.put("key", CardApp.key);

		LinkedHashMap<String, String> mapPramas = new LinkedHashMap<>();
		mapPramas.put("accountId", "");
		mapPramas.put("deviceCode", ds.getUUID());
		mapPramas.put("intefaceType",CardApp.intefaceType);
		mapPramas.put("mobile", num);
		mapPramas.put("passWord", MD5Encoder.encodeBase64(pass));
		mapPramas.put("sign", MD5Encoder.encode(SignUtil.getSign(mapSign)));
		mapPramas.put("source", CardApp.source);
		mapPramas.put("terminalId", "A");
		mapPramas.put("timeStamp", SystemUtil.getSystemTime());
		mapPramas.put("token", "");
		mapPramas.put("version", BuildConfig.VERSION_NAME);

		OkGo.<String>post(UrlsUtils.LOGIN)
				.params(mapPramas)
				.execute(
						new StringCallback() {
							@Override
							public void onSuccess(Response<String> response) {
								if (!TextUtils.isEmpty(response.body())) {
									JSONObject responseJson = JsonUtils.getJsonObject(response.body().toString());
									String code = TypeUtils.getJsonString(responseJson, "code", "");
									String message = TypeUtils.getJsonString(responseJson, "message", "");
									ToastUtil.showShortToast(message);
									if (!TextUtils.isEmpty(code) && "9999".equals(code)) {
										if (null != ds) {
											ds.setAccountId(TypeUtils.getJsonString(responseJson, "accountId", ""));
											ds.setToken(TypeUtils.getJsonString(responseJson, "token", ""));
											ds.setIsLog(true);
											ds.setAccount(TypeUtils.getJsonString(responseJson, "userMobile", ""));
										}
										SignActivity.this.finish();
									}
								}
								LogUtils.println("zhangyiyionSuccessSign------" + response.body());
							}

							@Override
							public void onError(Response<String> response) {
								ToastUtil.showShortToast("登录失败");
								LogUtils.println("zhangyiyionErrSign------" + response.body() + response.message() + response.code());
							}
						}
				);
	}
}
